<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>

<script language="JavaScript">
function openSaveChangesDialog()
{
  var isNewField = document.getElementById('adminCommand.isNew');
  if (isNewField.value == 'true')
  {
	afterFieldEdit('<spring:message code="field.name"/>', 'string', 'role.name', 'false', new Array());
  }
  setTimeout("checkErrorsForSaveChangesDialog()", 100);
}

function editableSelectAll()
{
  var index = 0;
  var selectAllEditable = document.getElementById("selectDeselectEditable");
  var editable = document.getElementById("listOfRoleUrlWebs" + index + ".editable1");

  while (editable != null)
  {
		if (selectAllEditable.checked && !editable.disabled)
		{
			editable.checked = "true";
			editable.value = "true";
		}
		else
		{
			editable.checked = "";
			editable.value = "false";
		}

		fieldEditCheckBox("listOfRoleUrlWebs" + index + ".editable1");

		index++;
		editable = document.getElementById("listOfRoleUrlWebs" + index + ".editable1");
  }
}

function accessSelectAll()
{
	var index = 0;
	var selectAllAccess = document.getElementById("selectDeselectAccess");
	var access = document.getElementById("listOfRoleUrlWebs" + index + ".access1");
	var editable = document.getElementById("listOfRoleUrlWebs" + index + ".editable1");

	while (access != null)
	{
		if (selectAllAccess.checked)
		{
			access.checked = true;
			access.value = true;
		}
		else
		{
			access.checked = "";
			access.value = false;
		}

		enabled(index, "listOfRoleUrlWebs" + index + ".access1");

		index++;
		access = document.getElementById("listOfRoleUrlWebs" + index + ".access1");
	}
}

function enabled(i, fieldName)
{
	var access = document.getElementById("listOfRoleUrlWebs" + i + ".access1");
	var editable = document.getElementById("listOfRoleUrlWebs" + i + ".editable1");
	var selectAllEditable = document.getElementById("selectDeselectEditable");

	if (access.checked)
	{
		editable.disabled = false;

		if (selectAllEditable.checked)
		{
			selectAllEditable.checked = false;
		}
	}
	else
	{
		editable.disabled = true;
		if (editable.checked)
		{
			editable.checked = false;
		}
		if (selectAllEditable.checked)
		{
			selectAllEditable.checked = false;
		}
	}

	fieldEditCheckBox(fieldName);

	if (fieldName.indexOf("editable") == -1)
	{
		fieldEditCheckBox(fieldName.replace("access", "editable"));
	}
}
</script>

<form id="adminForm" name="adminForm" method="post" commandName="adminCommand">

  <form:hidden path="adminCommand.adminRequestedAction" id="adminCommand.adminRequestedAction"/>
  <form:hidden path="adminCommand.isNew" id="adminCommand.isNew"/>
  <input type="hidden" id="callStatus" name="callStatus" value=""/>

  <tag:errors name="adminCommand"/>

  <table>
    <jsp:include page="/pages/jsp/tiles/admin/errors.jsp"/>
  </table>


  <table align="center" style="width:400px;max-width:400px;min-width:400px;">

    <security:authorize ifAllGranted="ROLE_EDITABLE">
    <c:choose>
	<c:when test="${!adminCommand.isNew}">
	<tr>
      <th>Name:</th>
	  <td>${adminCommand.role.name}</td>
	  <form:hidden path="adminCommand.role.name" id="adminCommand.role.name"/>
	</tr>
    <tr>
      <th>User's with this role:</th>
	  <td>
	    <c:forEach var="linkedUserName" items="${adminCommand.linkedUserNames}" varStatus="loopStatus">
        <c:choose>
        <c:when test="${loopStatus.index == 0}">
        ${linkedUserName}
        </c:when>
        <c:otherwise>
        , ${linkedUserName}
        </c:otherwise>
        </c:choose>
        </c:forEach>
      </td>
    </tr>
    <c:if test="${fn:length(adminCommand.linkedUserNames) > 0}">
	<tr>
	  <td colspan="2" style="color:red;">(The Role cannot be deleted as there are users linked to it)</td>
	</tr>
	</c:if>
  </c:when>
  <c:otherwise>
	<spring:bind path="adminCommand.role.name">
	<tr>
      <th><spring:message code="field.name"/></th>
	  <td>
      <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
      <input type="text"
             id="${status.expression}"
             name="${status.expression}" value="${status.value}" tabindex="1"
             class="largeTextField"
             maxlength="20"
             onkeyup="fieldEdit('${status.expression}');"
             onchange="afterFieldEdit('<spring:message code="field.role"/>', 'string', '${status.expression}', 'false', new Array());"/>&nbsp;*
      <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
	  </td>
    </tr>
    </spring:bind>

	</c:otherwise>
	</c:choose>

    <c:choose>
    <c:when test="${adminCommand.role.name == 'SYSADMIN'}">
    <tr>
	  <td colspan="2">
	    <table class="tableResults" cellspacing="0" cellpadding="5" style="padding-bottom: 10px;width:400px;max-width:400px;min-width:400px;">
		  <thead>
		    <tr>
			  <th align="center" width="250px">Page Name</th>
			  <th align="center" width="50px">Access</th>
			  <th align="center" width="50px">Editable</th>
			</tr>
		  </thead>
		  <tbody>
          <c:forEach items="${adminCommand.listOfRoleUrlWebs}" var="item" varStatus="loopStatus">
		    <tr>
			  <td width="250px">${item.url.description}</td>
			  <td width="50px">
                <form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].access" cssClass="checkBoxField" disabled="true"/>
		      </td>
			  <td width="50px">
			    <input type="hidden" id="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" name="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" value="${adminCommand.listOfRoleUrlWebs[loopStatus.index].editable}"/>
				<form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].editable" cssClass="checkBoxField" disabled="true"/>
			  </td>
			</tr>
		  </c:forEach>
		  </tbody>
	    </table>
	  </td>
	</tr>
    </c:when>
    <c:otherwise>
    <tr>
	    <td colspan="2">
	      <table class="tableResults" cellspacing="0" cellpadding="5" style="padding-bottom: 10px;width:400px;max-width:400px;min-width:400px;">
		      <thead>
			    <tr>
				    <th align="center" width="250px">Page Name</th>
				    <th align="center" width="50px">
				      Access
				      <input type="checkbox" value="false" id="selectDeselectAccess" onclick="accessSelectAll();"/>
				    </th>
				    <th align="center" width="50px">
				      Editable
				      <input type="checkbox" value="false" id="selectDeselectEditable" onclick="editableSelectAll();"/>
				    </th>
				  </tr>
		      </thead>
		      <tbody>
              <c:forEach items="${adminCommand.listOfRoleUrlWebs}" var="item" varStatus="loopStatus">
			    <tr>
				  <td width="250px">${item.url.description}</td>
				  <td width="50px">
                    <input type="hidden" id="listOfRoleUrlWebs${loopStatus.index}.access1_originalValue" name="listOfRoleUrlWebs${loopStatus.index}.access1_originalValue" value="${adminCommand.listOfRoleUrlWebs[loopStatus.index].access}"/>
				    <form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].access" cssClass="checkBoxField"
				                   onclick="enabled('${loopStatus.index}', 'listOfRoleUrlWebs${loopStatus.index}.access1');fieldEditCheckBox('listOfRoleUrlWebs${loopStatus.index}.access1');"/>
				  </td>

				  <c:choose>
				  <c:when test="${item.access}">
				  <td width="50px">
					<input type="hidden" id="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" name="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" value="${adminCommand.listOfRoleUrlWebs[loopStatus.index].editable}"/>
				    <form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].editable" disabled="false" cssClass="checkBoxField"
				                   onclick="enabled('${loopStatus.index}', 'listOfRoleUrlWebs${loopStatus.index}.editable1');fieldEditCheckBox('listOfRoleUrlWebs${loopStatus.index}.editable1');"/>
				  </td>
				  </c:when>
				  <c:otherwise>
				  <td width="50px">
					<input type="hidden" id="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" name="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" value="${adminCommand.listOfRoleUrlWebs[loopStatus.index].editable}"/>
				    <form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].editable" disabled="true" cssClass="checkBoxField"
				                     onclick="enabled('${loopStatus.index}', 'listOfRoleUrlWebs${loopStatus.index}.editable1');fieldEditCheckBox('listOfRoleUrlWebs${loopStatus.index}.editable1');"/>
				  </td>
				  </c:otherwise>
				  </c:choose>
			    </tr>
		      </c:forEach>
		      </tbody>
	      </table>
	    </td>
	  </tr>
    </c:otherwise>
    </c:choose>
    </security:authorize>

    <security:authorize ifNotGranted="ROLE_EDITABLE">
    <tr>
      <th>Name:</th>
	  <td>${adminCommand.role.name}</td>
	</tr>
	<tr>
      <th>User's with this role:</th>
	  <td>
	    <c:forEach var="linkedUserName" items="${adminCommand.linkedUserNames}" varStatus="loopStatus">
          <c:choose>
          <c:when test="${loopStatus.index == 0}">${linkedUserName}</c:when>
          <c:otherwise>, ${linkedUserName}</c:otherwise>
          </c:choose>
        </c:forEach>
      </td>
    </tr>
    <tr>
	  <td colspan="2">
	    <table class="tableResults" cellspacing="0" cellpadding="5" style="padding-bottom: 10px;width:400px;max-width:400px;min-width:400px;">
		  <thead>
		    <tr>
			  <th align="center" width="250px">Page Name</th>
			  <th align="center" width="50px">Access</th>
			  <th align="center" width="50px">Editable</th>
			</tr>
		  </thead>
		  <tbody>
          <c:forEach items="${adminCommand.listOfRoleUrlWebs}" var="item" varStatus="loopStatus">
		    <tr>
			  <td width="250px">${item.url.description}</td>
			  <td width="50px">
                <form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].access" cssClass="checkBoxField" disabled="true"/>
		      </td>
			  <td width="50px">
			    <input type="hidden" id="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" name="listOfRoleUrlWebs${loopStatus.index}.editable1_originalValue" value="${adminCommand.listOfRoleUrlWebs[loopStatus.index].editable}"/>
				<form:checkbox path="adminCommand.listOfRoleUrlWebs[${loopStatus.index}].editable" cssClass="checkBoxField" disabled="true"/>
			  </td>
			</tr>
		  </c:forEach>
		  </tbody>
	    </table>
	  </td>
	</tr>
    </security:authorize>

  </table>
  <table align="center" style="width:600px;max-width:600px;min-width:600px;">
	<tr>
      <td colspan="2">
        <table align="center"><tr><td><jsp:include page="/pages/jsp/tiles/admin/roleEditButtons.jsp"/></td></tr></table>
	  </td>
	</tr>
    <jsp:include page="/pages/jsp/tiles/admin/hoverConfirmation.jsp"/>
  </table>
</form>